using Microsoft.NetEnterpriseServers;
using Microsoft.SqlServer.Management.SqlMgmt;
using Microsoft.SqlServer.Management.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Windows.Forms;

namespace Microsoft.SqlServer.Management.SqlManagerUI
{
	public class ProviderDetailDialog : Form
	{
		private ProviderEntity _provider;

		private DatabaseDetailDialog _databaseDetailDialog;

		private IMessageBoxProvider _messageProvider;

		private bool _isCheckedForChanges;

		private bool _isChanged;

		private bool _isOKSelected;

		private List<DatabaseEntity> _databases;

		private WizardPage _parent;

		private IContainer components;

		private Label lblProviderName;

		private Label lblWebServiceAddress;

		private TextBox txtProviderName;

		private TextBox txtWebServiceAddress;

		private GroupBox grpAuthentication;

		private CheckBox chbSavePassword;

		private Label lblPassword;

		private Label lblUsername;

		private TextBox txtPassword;

		private TextBox txtUsername;

		private GroupBox grpDatabases;

		private ListBox lsbDatabases;

		private Panel pnlButtonsPanel;

		private Button btnDownloadConfg;

		private Button btnSetDefault;

		private Button btnDelete;

		private Button btnEdit;

		private Button btnNew;

		private Panel pnlFooterButtons;

		private Button btnOk;

		private Button btnCancel;

		public WizardPage WizardParent
		{
			get
			{
				return this._parent;
			}
		}

		public bool IsOkSelected
		{
			get
			{
				return this._isOKSelected;
			}
		}

		public ProviderEntity Provider
		{
			get
			{
				return this._provider;
			}
			set
			{
				this._provider = value;
				this._provider.Databases.Sort(new DatabasesComparer());
				this._databases = new List<DatabaseEntity>();
				foreach (DatabaseEntity current in this._provider.Databases)
				{
					this._databases.Add((DatabaseEntity)current.Clone());
				}
				this.BindControls();
			}
		}

		private DatabaseDetailDialog DatabaseDetailDialog
		{
			get
			{
				if (this._databaseDetailDialog == null)
				{
					this._databaseDetailDialog = new DatabaseDetailDialog(this.WizardParent, this._messageProvider);
				}
				return this._databaseDetailDialog;
			}
		}

		private DatabaseEntity SelectedDatabase
		{
			get
			{
				if (this.lsbDatabases.SelectedItem != null)
				{
					int selectedIndex = this.lsbDatabases.SelectedIndex;
					if (selectedIndex >= 0)
					{
						return (DatabaseEntity)this.lsbDatabases.SelectedItem;
					}
				}
				return null;
			}
		}

		public ProviderDetailDialog(WizardPage parent, IMessageBoxProvider messageProvider)
		{
			this.InitializeComponent();
			this._messageProvider = messageProvider;
			this._parent = parent;
			base.Icon = CUtils.LoadIcon("server.ico");
		}

		private void LoadStrings()
		{
			if (!string.IsNullOrEmpty(this._provider.Name))
			{
				this.Text = string.Format(CultureInfo.CurrentCulture, GenScriptWizSR.ProviderDetailTitle, new object[]
				{
					this._provider.Name
				});
			}
			else
			{
				this.Text = string.Format(CultureInfo.CurrentCulture, GenScriptWizSR.ProviderDetailTitle, new object[]
				{
					GenScriptWizSR.ProviderDetailNewProvider
				});
			}
			this.lblProviderName.Text = GenScriptWizSR.ProviderDetailProviderName;
			this.lblPassword.Text = GenScriptWizSR.ProviderDetailWebSerPassword;
			this.lblUsername.Text = GenScriptWizSR.ProviderDetailWebSerUser;
			this.grpAuthentication.Text = GenScriptWizSR.ProviderDetailWebSerAuth;
			this.chbSavePassword.Text = GenScriptWizSR.ProviderDetailWebSerSavePassword;
			this.grpDatabases.Text = GenScriptWizSR.ProviderDetailDatabaseGroup;
			this.btnCancel.Text = GenScriptWizSR.CancelBtn;
			this.btnDelete.Text = GenScriptWizSR.DeleteText;
			this.btnEdit.Text = GenScriptWizSR.EditText;
			this.btnNew.Text = GenScriptWizSR.NewText;
			this.btnSetDefault.Text = GenScriptWizSR.SetAsDefaultText;
			this.btnDownloadConfg.Text = GenScriptWizSR.DownloadConfigurationsText;
			this.btnOk.Text = GenScriptWizSR.OK;
			this.lblWebServiceAddress.Text = GenScriptWizSR.ProviderDetailWebAdd;
		}

		private void BindDatabaseList()
		{
			this.lsbDatabases.DataSource = null;
			if (this._databases != null)
			{
				this.lsbDatabases.DataSource = this._databases;
				this.lsbDatabases.DisplayMember = "FullName";
				this.lsbDatabases.DrawItem += new DrawItemEventHandler(this.lsbDatabases_DrawItem);
			}
		}

		private void lsbDatabases_DrawItem(object sender, DrawItemEventArgs e)
		{
			if (e.Index >= 0)
			{
				e.DrawBackground();
				DatabaseEntity databaseEntity = (DatabaseEntity)this.lsbDatabases.Items[e.Index];
				Font font = e.Font;
				Brush brush = new SolidBrush(e.ForeColor);
				if (databaseEntity.IsDefault)
				{
					font = new Font(e.Font, FontStyle.Bold);
				}
				e.Graphics.DrawString(databaseEntity.FullName, font, brush, e.Bounds, StringFormat.GenericDefault);
				e.DrawFocusRectangle();
			}
		}

		private void BindControls()
		{
			this.txtProviderName.Text = this._provider.Name;
			this.txtWebServiceAddress.Text = this._provider.WebServiceAddress;
			this.txtUsername.Text = this._provider.UserName;
			if (this._provider.SavePassword)
			{
				this.txtPassword.Text = this._provider.Password;
			}
			else
			{
				this.txtPassword.Text = string.Empty;
			}
			this.chbSavePassword.Checked = this._provider.SavePassword;
			this.BindDatabaseList();
		}

		private bool SaveChanges()
		{
			if (this.ValidateFields())
			{
				this._provider.Name = this.txtProviderName.Text;
				this._provider.WebServiceAddress = this.txtWebServiceAddress.Text;
				this._provider.UserName = this.txtUsername.Text;
				this._provider.Password = this.txtPassword.Text;
				this._provider.SavePassword = this.chbSavePassword.Checked;
				this._provider.Databases.Clear();
				foreach (DatabaseEntity current in this._databases)
				{
					this._provider.Databases.Add(current);
				}
				if (this._provider.Databases.Count == 1)
				{
					this._provider.Databases[0].IsDefault = true;
				}
				this._isOKSelected = true;
				this._isChanged = false;
				return true;
			}
			return false;
		}

		private void AbortChanges()
		{
			this._isChanged = false;
		}

		private void CloseDialog()
		{
			base.Close();
		}

		private bool EditDatabase(DatabaseEntity database)
		{
			this.DatabaseDetailDialog.Database = database;
			this.DatabaseDetailDialog.ShowDialog(this);
			return true;
		}

		private void DeleteDatabase(DatabaseEntity database)
		{
			this._databases.Remove(database);
		}

		private void SetDefaultDatabase(DatabaseEntity database)
		{
			foreach (DatabaseEntity current in this._databases)
			{
				if (current.IsDefault)
				{
					current.IsDefault = false;
				}
			}
			database.IsDefault = true;
		}

		private void ProviderFieldsTextChanged(object sender, EventArgs e)
		{
			if (!this._isChanged)
			{
				this._isChanged = true;
			}
		}

		private bool ValidateFields()
		{
			bool result = true;
			string text = null;
			if (string.IsNullOrEmpty(this.txtProviderName.Text))
			{
				text = GenScriptWizSR.ProviderDetailProviderName;
			}
			else if (string.IsNullOrEmpty(this.txtWebServiceAddress.Text))
			{
				text = GenScriptWizSR.ProviderDetailWebAdd;
			}
			else if (string.IsNullOrEmpty(this.txtUsername.Text))
			{
				text = GenScriptWizSR.ProviderDetailWebSerUser;
			}
			if (!string.IsNullOrEmpty(text))
			{
				this._messageProvider.ShowMessage(string.Format(CultureInfo.CurrentCulture, GenScriptWizSR.ProviderDetailEmptyProviderNameMessage, new object[]
				{
					text
				}), GenScriptWizSR.ProviderDetailEmptyProviderNameMessageTitle, ExceptionMessageBoxButtons.OK, ExceptionMessageBoxSymbol.Warning, this.WizardParent);
				result = false;
			}
			return result;
		}

		private void chbSavePassword_CheckedChanged(object sender, EventArgs e)
		{
			if (this.chbSavePassword.Checked)
			{
			}
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			if (this.SaveChanges())
			{
				this.CloseDialog();
			}
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			this._isCheckedForChanges = true;
			if (this.CheckChangesForSaving())
			{
				this.AbortChanges();
				this.CloseDialog();
			}
		}

		private void btnNew_Click(object sender, EventArgs e)
		{
			DatabaseEntity databaseEntity = new DatabaseEntity();
			if (this.EditDatabase(databaseEntity) && !string.IsNullOrEmpty(databaseEntity.DatabaseName))
			{
				this._isChanged = true;
				this._databases.Add(databaseEntity);
				this.BindDatabaseList();
			}
		}

		private void btnEdit_Click(object sender, EventArgs e)
		{
			DatabaseEntity selectedDatabase = this.SelectedDatabase;
			if (selectedDatabase != null && this.EditDatabase(selectedDatabase))
			{
				this.BindDatabaseList();
			}
		}

		private void btnDelete_Click(object sender, EventArgs e)
		{
			DatabaseEntity selectedDatabase = this.SelectedDatabase;
			if (selectedDatabase != null)
			{
				DialogResult dialogResult = this._messageProvider.ShowMessage(GenScriptWizSR.ProviderDetailDeleteMessage, GenScriptWizSR.ProviderDetailDeleteTitle, ExceptionMessageBoxButtons.YesNoCancel, ExceptionMessageBoxSymbol.Question, this.WizardParent);
				if (dialogResult == DialogResult.Yes)
				{
					this.DeleteDatabase(selectedDatabase);
					this.BindDatabaseList();
				}
			}
		}

		private void btnSetDefault_Click(object sender, EventArgs e)
		{
			DatabaseEntity selectedDatabase = this.SelectedDatabase;
			if (selectedDatabase != null)
			{
				this.SetDefaultDatabase(selectedDatabase);
				this.BindDatabaseList();
			}
		}

		private void btnDownloadConfg_Click(object sender, EventArgs e)
		{
		}

		private void ProviderDetailDialog_Load(object sender, EventArgs e)
		{
			this.txtProviderName.Select();
			this._isChanged = false;
			this.LoadStrings();
			this._isOKSelected = false;
			this.txtProviderName.TextChanged += new EventHandler(this.ProviderFieldsTextChanged);
			this.txtWebServiceAddress.TextChanged += new EventHandler(this.ProviderFieldsTextChanged);
			this.txtPassword.TextChanged += new EventHandler(this.ProviderFieldsTextChanged);
			this.txtUsername.TextChanged += new EventHandler(this.ProviderFieldsTextChanged);
		}

		private bool CheckChangesForSaving()
		{
			bool result = false;
			if (this._isChanged)
			{
				DialogResult dialogResult = this._messageProvider.ShowMessage(string.Format(CultureInfo.CurrentCulture, GenScriptWizSR.ProviderDetailSaveChangeMessage, new object[]
				{
					this._provider.Name
				}), GenScriptWizSR.ProviderDetailSaveChangeTitle, ExceptionMessageBoxButtons.YesNoCancel, ExceptionMessageBoxSymbol.Question, this.WizardParent);
				if (dialogResult == DialogResult.Yes)
				{
					result = this.SaveChanges();
				}
				else if (dialogResult == DialogResult.No)
				{
					this.AbortChanges();
					result = true;
				}
				else if (dialogResult == DialogResult.Cancel)
				{
					result = false;
				}
			}
			else
			{
				result = true;
			}
			return result;
		}

		private void ProviderDetailDialog_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (!this._isCheckedForChanges)
			{
				if (!this.CheckChangesForSaving())
				{
					e.Cancel = true;
				}
			}
			else if (this._isChanged)
			{
				e.Cancel = true;
			}
			this._isCheckedForChanges = false;
		}

		protected override void Dispose(bool disposing)
		{
			if (disposing && this.components != null)
			{
				this.components.Dispose();
			}
			base.Dispose(disposing);
		}

		private void InitializeComponent()
		{
			this.lblProviderName = new Label();
			this.lblWebServiceAddress = new Label();
			this.txtProviderName = new TextBox();
			this.txtWebServiceAddress = new TextBox();
			this.grpAuthentication = new GroupBox();
			this.chbSavePassword = new CheckBox();
			this.lblPassword = new Label();
			this.lblUsername = new Label();
			this.txtPassword = new TextBox();
			this.txtUsername = new TextBox();
			this.grpDatabases = new GroupBox();
			this.pnlButtonsPanel = new Panel();
			this.btnDownloadConfg = new Button();
			this.btnSetDefault = new Button();
			this.btnDelete = new Button();
			this.btnEdit = new Button();
			this.btnNew = new Button();
			this.lsbDatabases = new ListBox();
			this.pnlFooterButtons = new Panel();
			this.btnOk = new Button();
			this.btnCancel = new Button();
			this.grpAuthentication.SuspendLayout();
			this.grpDatabases.SuspendLayout();
			this.pnlButtonsPanel.SuspendLayout();
			this.pnlFooterButtons.SuspendLayout();
			base.SuspendLayout();
			this.lblProviderName.AutoSize = true;
			this.lblProviderName.Location = new Point(13, 13);
			this.lblProviderName.Name = "lblProviderName";
			this.lblProviderName.Size = new Size(22, 13);
			this.lblProviderName.TabIndex = 0;
			this.lblProviderName.Text = "A:OK";
			this.lblWebServiceAddress.AutoSize = true;
			this.lblWebServiceAddress.Location = new Point(13, 34);
			this.lblWebServiceAddress.Name = "lblWebServiceAddress";
			this.lblWebServiceAddress.Size = new Size(107, 13);
			this.lblWebServiceAddress.TabIndex = 0;
			this.lblWebServiceAddress.Text = "A:Web service address";
			this.txtProviderName.Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right);
			this.txtProviderName.Location = new Point(124, 10);
			this.txtProviderName.Name = "txtProviderName";
			this.txtProviderName.Size = new Size(293, 20);
			this.txtProviderName.TabIndex = 0;
			this.txtWebServiceAddress.Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right);
			this.txtWebServiceAddress.Location = new Point(61, 49);
			this.txtWebServiceAddress.Name = "txtWebServiceAddress";
			this.txtWebServiceAddress.Size = new Size(356, 20);
			this.txtWebServiceAddress.TabIndex = 1;
			this.grpAuthentication.Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right);
			this.grpAuthentication.Controls.Add(this.chbSavePassword);
			this.grpAuthentication.Controls.Add(this.lblPassword);
			this.grpAuthentication.Controls.Add(this.lblUsername);
			this.grpAuthentication.Controls.Add(this.txtPassword);
			this.grpAuthentication.Controls.Add(this.txtUsername);
			this.grpAuthentication.Location = new Point(16, 87);
			this.grpAuthentication.Name = "grpAuthentication";
			this.grpAuthentication.Size = new Size(407, 100);
			this.grpAuthentication.TabIndex = 2;
			this.grpAuthentication.TabStop = false;
			this.grpAuthentication.Text = "A:Web service authentication";
			this.chbSavePassword.AutoSize = true;
			this.chbSavePassword.Location = new Point(18, 77);
			this.chbSavePassword.Name = "chbSavePassword";
			this.chbSavePassword.Size = new Size(99, 17);
			this.chbSavePassword.TabIndex = 4;
			this.chbSavePassword.Text = "A:Save password";
			this.chbSavePassword.UseVisualStyleBackColor = true;
			this.chbSavePassword.CheckedChanged += new EventHandler(this.chbSavePassword_CheckedChanged);
			this.lblPassword.AutoSize = true;
			this.lblPassword.Location = new Point(6, 42);
			this.lblPassword.Name = "lblPassword";
			this.lblPassword.Size = new Size(53, 13);
			this.lblPassword.TabIndex = 0;
			this.lblPassword.Text = "A:Password";
			this.lblUsername.AutoSize = true;
			this.lblUsername.Location = new Point(6, 16);
			this.lblUsername.Name = "lblUsername";
			this.lblUsername.Size = new Size(58, 13);
			this.lblUsername.TabIndex = 0;
			this.lblUsername.Text = "A:User name";
			this.txtPassword.Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right);
			this.txtPassword.Location = new Point(108, 42);
			this.txtPassword.Name = "txtPassword";
			this.txtPassword.PasswordChar = '*';
			this.txtPassword.Size = new Size(293, 20);
			this.txtPassword.TabIndex = 3;
			this.txtUsername.Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right);
			this.txtUsername.Location = new Point(108, 16);
			this.txtUsername.Name = "txtUsername";
			this.txtUsername.Size = new Size(293, 20);
			this.txtUsername.TabIndex = 2;
			this.grpDatabases.Anchor = (AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right);
			this.grpDatabases.Controls.Add(this.pnlButtonsPanel);
			this.grpDatabases.Controls.Add(this.lsbDatabases);
			this.grpDatabases.Location = new Point(16, 198);
			this.grpDatabases.Name = "grpDatabases";
			this.grpDatabases.Size = new Size(407, 201);
			this.grpDatabases.TabIndex = 4;
			this.grpDatabases.TabStop = false;
			this.grpDatabases.Text = "A:Available databases";
			this.pnlButtonsPanel.Anchor = (AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Right);
			this.pnlButtonsPanel.Controls.Add(this.btnDownloadConfg);
			this.pnlButtonsPanel.Controls.Add(this.btnSetDefault);
			this.pnlButtonsPanel.Controls.Add(this.btnDelete);
			this.pnlButtonsPanel.Controls.Add(this.btnEdit);
			this.pnlButtonsPanel.Controls.Add(this.btnNew);
			this.pnlButtonsPanel.Location = new Point(282, 19);
			this.pnlButtonsPanel.Name = "pnlButtonsPanel";
			this.pnlButtonsPanel.Size = new Size(117, 176);
			this.pnlButtonsPanel.TabIndex = 6;
			this.btnDownloadConfg.Location = new Point(6, 136);
			this.btnDownloadConfg.Name = "btnDownloadConfg";
			this.btnDownloadConfg.Size = new Size(108, 37);
			this.btnDownloadConfg.TabIndex = 4;
			this.btnDownloadConfg.Text = "A:Download configurations";
			this.btnDownloadConfg.UseVisualStyleBackColor = true;
			this.btnDownloadConfg.Visible = false;
			this.btnDownloadConfg.Click += new EventHandler(this.btnDownloadConfg_Click);
			this.btnSetDefault.Location = new Point(5, 90);
			this.btnSetDefault.Name = "btnSetDefault";
			this.btnSetDefault.Size = new Size(108, 40);
			this.btnSetDefault.TabIndex = 3;
			this.btnSetDefault.Text = "A:Set as default";
			this.btnSetDefault.UseVisualStyleBackColor = true;
			this.btnSetDefault.Click += new EventHandler(this.btnSetDefault_Click);
			this.btnDelete.Location = new Point(5, 61);
			this.btnDelete.Name = "btnDelete";
			this.btnDelete.Size = new Size(108, 23);
			this.btnDelete.TabIndex = 2;
			this.btnDelete.Text = "A:Delete";
			this.btnDelete.UseVisualStyleBackColor = true;
			this.btnDelete.Click += new EventHandler(this.btnDelete_Click);
			this.btnEdit.Location = new Point(6, 32);
			this.btnEdit.Name = "btnEdit";
			this.btnEdit.Size = new Size(108, 23);
			this.btnEdit.TabIndex = 1;
			this.btnEdit.Text = "A:Edit...";
			this.btnEdit.UseVisualStyleBackColor = true;
			this.btnEdit.Click += new EventHandler(this.btnEdit_Click);
			this.btnNew.Location = new Point(6, 3);
			this.btnNew.Name = "btnNew";
			this.btnNew.Size = new Size(108, 23);
			this.btnNew.TabIndex = 0;
			this.btnNew.Text = "A:New...";
			this.btnNew.UseVisualStyleBackColor = true;
			this.btnNew.Click += new EventHandler(this.btnNew_Click);
			this.lsbDatabases.Anchor = (AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right);
			this.lsbDatabases.DrawMode = DrawMode.OwnerDrawFixed;
			this.lsbDatabases.FormattingEnabled = true;
			this.lsbDatabases.Location = new Point(9, 19);
			this.lsbDatabases.Name = "lsbDatabases";
			this.lsbDatabases.Size = new Size(267, 173);
			this.lsbDatabases.Sorted = true;
			this.lsbDatabases.TabIndex = 5;
			this.pnlFooterButtons.Anchor = (AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right);
			this.pnlFooterButtons.Controls.Add(this.btnOk);
			this.pnlFooterButtons.Controls.Add(this.btnCancel);
			this.pnlFooterButtons.Location = new Point(16, 406);
			this.pnlFooterButtons.Name = "pnlFooterButtons";
			this.pnlFooterButtons.Size = new Size(412, 29);
			this.pnlFooterButtons.TabIndex = 7;
			this.btnOk.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right);
			this.btnOk.Location = new Point(249, 3);
			this.btnOk.Name = "btnOk";
			this.btnOk.Size = new Size(75, 23);
			this.btnOk.TabIndex = 0;
			this.btnOk.Text = "A:OK";
			this.btnOk.UseVisualStyleBackColor = true;
			this.btnOk.Click += new EventHandler(this.btnOk_Click);
			this.btnCancel.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right);
			this.btnCancel.DialogResult = DialogResult.Cancel;
			this.btnCancel.Location = new Point(330, 3);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new Size(75, 23);
			this.btnCancel.TabIndex = 1;
			this.btnCancel.Text = "A:Cancel";
			this.btnCancel.UseVisualStyleBackColor = true;
			this.btnCancel.Click += new EventHandler(this.btnCancel_Click);
			base.AcceptButton = this.btnOk;
			base.AutoScaleDimensions = new SizeF(6f, 13f);
			base.AutoScaleMode = AutoScaleMode.Font;
			base.CancelButton = this.btnCancel;
			base.ClientSize = new Size(440, 440);
			base.Controls.Add(this.pnlFooterButtons);
			base.Controls.Add(this.grpDatabases);
			base.Controls.Add(this.grpAuthentication);
			base.Controls.Add(this.txtWebServiceAddress);
			base.Controls.Add(this.txtProviderName);
			base.Controls.Add(this.lblWebServiceAddress);
			base.Controls.Add(this.lblProviderName);
			this.MinimumSize = new Size(448, 474);
			base.Name = "ProviderDetailDialog";
			base.ShowInTaskbar = false;
			base.StartPosition = FormStartPosition.CenterParent;
			this.Text = "A:Provider configuration for {0}";
			base.FormClosing += new FormClosingEventHandler(this.ProviderDetailDialog_FormClosing);
			base.Load += new EventHandler(this.ProviderDetailDialog_Load);
			this.grpAuthentication.ResumeLayout(false);
			this.grpAuthentication.PerformLayout();
			this.grpDatabases.ResumeLayout(false);
			this.pnlButtonsPanel.ResumeLayout(false);
			this.pnlFooterButtons.ResumeLayout(false);
			base.ResumeLayout(false);
			base.PerformLayout();
		}
	}
}
